<template>
	<view>
		<view class="logo">
			<view class="img">
				<image mode="widthFix" src="/static/img/logo.png">
				</image>
			</view>
		</view>
		<!-- 账号密码输入框 -->
		<view class="form">
			<view class="btn" @tap="doLogin">授 权 登 录</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phoneNumber: '',
				passwd: '',
				isShowOauth: false,
				showProvider: {
					weixin: false,
					qq: false,
					sinaweibo: false,
					xiaomi: false
				}
			}
		},
		onShow() {

		},
		onLoad() {
			//APP显示第三方登录
			// #ifdef APP-PLUS
			this.isShowOauth = true;
			// #endif
			this.getProvider();

		},
		methods: {
			getProvider() {
				//获取第三方登录服务
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						let len = res.provider.length;
						for (let i = 0; i < len; i++) {
							//有服务才显示按钮图标
							this.showProvider[res.provider[i]] = true;
						}
						if (res.provider.length == 0) {
							this.isShowOauth = false;
						}
					}
				});
			},
			toPage(page) {
				uni.hideKeyboard()
				uni.navigateTo({
					url: page
				});
			},
			doLogin() {
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						console.log(res)
						if (~res.provider.indexOf('weixin')) {
							uni.getUserProfile({
								desc: "登录",
								success: (res) => {
									console.log(res)
									uni.login({
										success: (info) => {
											const code = info.code
											console.log(code)
											const encryptedData = res.encryptedData
											const iv = res.iv
											uni.request({
												url: getApp().globalData.data
													.requestUrl +
													"/Open/getUserInfo",
												data: {
													code: code,
													encryptedData: encryptedData,
													iv: iv,
												},
												success: (res2) => {
													console.log(res2)
													if (res2.data.status ==
														1) {
														if (res2.data.data
															.is_bind) {
															uni.setStorageSync(
																'token',
																res2
																.data
																.data
																.token)
															console.log(uni
																.getStorageSync(
																	'token'
																))
															uni.switchTab({
																url: '/pages/tabBar/user/user'
															})
														} else {
															this.token =
																res2.data
																.data.token
															uni.redirectTo({
																url: "/pages/login/phone?token=" +
																	this
																	.token
															})
														}
													} else {
														uni.showToast({
															title: res2
																.data
																.message,
															icon: 'error'
														})
													}
												},
												fail: (err) => {
													console.log(err)
												}
											})
										}
									})
								}
							})

						} else {
							uni.showToast({
								title: '请先安装微信或升级版本',
								icon: "none"
							});

						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/static/css/login.scss";

	.form {
		.res {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100upx;
			color: rgba($color: #ffffff, $alpha: 0.8);
		}
	}

	.oauth {
		@media all and (max-height:150vw) {
			display: none;
		}

		position: absolute;
		bottom: 50upx;
		width: 100%;

		.text {
			width: 100%;
			height: 60upx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: rgba($color: #ffffff, $alpha: 0.8);
			font-size: 28upx;
		}

		.list {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 20upx 0;

			.icon {
				font-size: 80upx;
				margin: 0 30upx;
			}
		}
	}
</style>
